﻿using System;

namespace Listing1_85
{
    public class Program
    {
        static void Main()
        {
            Pub p = new Pub();
            p.onChange += (sender, e)
            => Console.WriteLine("Event raised: {0}", e.Value);
            p.Raise();
            Console.ReadKey();
        }
    }

    public class MyArgs : EventArgs
    {
        public MyArgs(int value)
        {
            Value = value;
        }
        public int Value { get; set; }
    }

    public class Pub
    {
        public event EventHandler<MyArgs> onChange = delegate { };

        public event EventHandler<MyArgs> OnChange
        {
            add
            {
                lock (onChange)
                {
                    onChange += value;
                }
            }
            remove
            {
                lock (onChange)
                {
                    onChange -= value;
                }
            }
        }

        public void Raise()
        {
            onChange(this, new MyArgs(42));
        }
    }
}
